//
//  HomeTableCellOfPhoto.m
//  aMoment
//
//  Created by dllo on 15/10/7.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "HomeTableCellOfPhoto.h"
#import "AllHeaderFiles.h"

@implementation HomeTableCellOfPhoto

- (void)dealloc
{
    [_nameLabel release];
    [_aboveImageView release];
    [_leftImageView release];
    [_rightImageView release];
    [_photoNumLabel release];
    [_titleLabel release];
    [_contentLabel release];
    [_likeNumField release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
    }
    return self;
}

- (void)createViews
{
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    [self.contentView addSubview:_nameLabel];
    [_nameLabel release];
    
    _aboveImageView = [[UIImageView alloc] init];
    _aboveImageView.backgroundColor = [UIColor grayColor];
    _aboveImageView.contentMode = UIViewContentModeScaleAspectFill;
    _aboveImageView.clipsToBounds = YES;
    [self.contentView addSubview:_aboveImageView];
    [_aboveImageView release];
    
    _leftImageView = [[UIImageView alloc] init];
    _leftImageView.backgroundColor = [UIColor grayColor];
    _leftImageView.contentMode = UIViewContentModeScaleAspectFill;
    _leftImageView.clipsToBounds = YES;
    [self.contentView addSubview:_leftImageView];
    [_leftImageView release];
    
    _rightImageView = [[UIImageView alloc] init];
    _rightImageView.backgroundColor = [UIColor grayColor];
    _rightImageView.contentMode = UIViewContentModeScaleAspectFill;
    _rightImageView.clipsToBounds = YES;
    [self.contentView addSubview:_rightImageView];
    [_rightImageView release];
    
    _photoNumLabel = [[UILabel alloc] init];
    _photoNumLabel.backgroundColor = [UIColor darkGrayColor];
    _photoNumLabel.textAlignment = NSTextAlignmentCenter;
    _photoNumLabel.font = [UIFont systemFontOfSize:FONTSIZE_MIDDLE];
    _photoNumLabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:_photoNumLabel];
    [_photoNumLabel release];
    for (NSInteger i = 0; i < 3; i++) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageViewAction:)];
        switch (i) {
            case 0:
            {
                _aboveImageView.userInteractionEnabled = YES;
                [_aboveImageView addGestureRecognizer:tap];
            }
                break;
            case 1:
            {
                _leftImageView.userInteractionEnabled = YES;
                [_leftImageView addGestureRecognizer:tap];
            }
                break;
            case 2:
            {
                _rightImageView.userInteractionEnabled = YES;
                [_rightImageView addGestureRecognizer:tap];
            }
                break;
                
            default:
                break;
        }
        [tap release];
    }
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel release];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = [UIFont systemFontOfSize:FONTSIZE_MIDDLE];
    _contentLabel.numberOfLines = 0;
    [self.contentView addSubview:_contentLabel];
    [_contentLabel release];
    
    _likeNumField = [[UITextField alloc] init];
    _likeNumField.enabled = NO;
    _likeNumField.textColor = FONTCOLOR_GRAY;
    _likeNumField.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IMAGE_16, IMAGE_16)];
    leftImageView.image = [UIImage imageNamed:@"iconfont-love"];
    _likeNumField.leftView = leftImageView;
    _likeNumField.leftViewMode = UITextFieldViewModeAlways;
    [leftImageView release];
    [self.contentView addSubview:_likeNumField];
    [_likeNumField release];
}

#pragma mark 点击图片方法
- (void)clickImageViewAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"000");
    FullScreenViewController *fullVC = [[FullScreenViewController alloc] init];
    fullVC.imageArray = _home.imglist;
    fullVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [(DDMenuController *)[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:fullVC animated:YES completion:nil];
}

- (void)layoutSubviews
{
    [super layoutSubviews];//_aboveImageView.Width / 2 - 2.5 * FIT_WIDTH
    _nameLabel.frame = CGRectMake(20 * FIT_WIDTH, 15 * FIT_HEIGHT, SCREEN_WIDTH - 40 * FIT_WIDTH, 20 * FIT_HEIGHT);
    _aboveImageView.frame = CGRectMake(_nameLabel.X, _nameLabel.Y + _nameLabel.Height + 15 * FIT_HEIGHT, _nameLabel.Width, 165 * FIT_WIDTH);
    _leftImageView.frame = CGRectMake(_aboveImageView.X, _aboveImageView.Y + _aboveImageView.Height + 5 * FIT_WIDTH, _aboveImageView.Height, _aboveImageView.Height);
    _rightImageView.frame = CGRectMake(_leftImageView.X + _leftImageView.Width + 5 * FIT_WIDTH, _leftImageView.Y, _leftImageView.Width, _leftImageView.Height);
    _photoNumLabel.frame = CGRectMake(_rightImageView.X + _rightImageView.Width - 50 * FIT_WIDTH, _rightImageView.Y + _rightImageView.Height - 40 * FIT_HEIGHT, 40 * FIT_WIDTH, 30 * FIT_HEIGHT);
    _titleLabel.frame = CGRectMake(_leftImageView.X, _leftImageView.Y + _leftImageView.Height + 15 * FIT_HEIGHT, _nameLabel.Width, _nameLabel.Height);
    _contentLabel.frame = CGRectMake(_titleLabel.X, _titleLabel.Y + _titleLabel.Height + 15 * FIT_HEIGHT, _titleLabel.Width, self.contentView.Height - (155 * FIT_HEIGHT + 330 * FIT_WIDTH));
    _likeNumField.frame = CGRectMake(_contentLabel.X + _contentLabel.Width - 60 * FIT_WIDTH, _contentLabel.Y + _contentLabel.Height + 15 * FIT_HEIGHT, 60 * FIT_WIDTH, _nameLabel.Height);
}

- (void)setHome:(Home *)home
{
    if (_home != home) {
        [_home release];
        _home = [home retain];
    }
    _nameLabel.text = [NSString stringWithFormat:@"%@ · %@", _home.name, _home.enname];
//    [_aboveImageView setImageWithURL:[NSURL URLWithString:[_home.imglist[0] objectForKey:@"imgurl"]]];
//    [_leftImageView setImageWithURL:[NSURL URLWithString:[_home.imglist[1] objectForKey:@"imgurl"]]];
//    [_rightImageView setImageWithURL:[NSURL URLWithString:[_home.imglist[2] objectForKey:@"imgurl"]]];
    [_aboveImageView setImageWithURL:[NSURL URLWithString:[_home.imglist[0] objectForKey:@"imgurl"]] placeholderImage:[UIImage imageNamed:@"placeholder-image"]];
    [_leftImageView setImageWithURL:[NSURL URLWithString:[_home.imglist[1] objectForKey:@"imgurl"]] placeholderImage:[UIImage imageNamed:@"placeholder-image"]];
    [_rightImageView setImageWithURL:[NSURL URLWithString:[_home.imglist[2] objectForKey:@"imgurl"]] placeholderImage:[UIImage imageNamed:@"placeholder-image"]];
    _photoNumLabel.text = [NSString stringWithFormat:@"%@", _home.imgtotal];
    _titleLabel.text = _home.title;
    _contentLabel.text = _home.content;
    _likeNumField.text = [NSString stringWithFormat:@" %@", _home.like];
    
//    NSString *likeNumString = [NSString stringWithFormat:@" %@", _home.like];
//    if (![likeNumString isEqual:@" 0"]) {
//        _likeNumField.text = likeNumString;
//    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
